package com.rongcheng.fortestlist.EXELFrameworkDemo;

import android.content.res.AssetManager;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.rongcheng.fortestlist.EXELFrameworkDemo.fragment.QABaseFragment;
import com.rongcheng.fortestlist.EXELFrameworkDemo.model.QANode;
import com.rongcheng.fortestlist.EXELFrameworkDemo.utils.EXELContaint;
import com.rongcheng.fortestlist.EXELFrameworkDemo.utils.FragmentCreator;
import com.rongcheng.fortestlist.R;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class ExElQAActivity extends AppCompatActivity {
    ViewPager view_pager;
    FragmentCreator fragmentCreator = FragmentCreator.getInstance();
    private static final Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    private ArrayList<QANode> nodes;
private FrameLayout card_fragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex_el_qa);
        initView();
        initDate();
        view_pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return QABaseFragment.getFragment(nodes.get(position));
            }

            @Override
            public int getCount() {
                return nodes.size();
            }

            @Override
            public Parcelable saveState() {
                return null;
            }
        });

    }

    private void initView() {
        card_fragment= (FrameLayout) findViewById(R.id.card_fragment);
        view_pager = (ViewPager) findViewById(R.id.view_pager);

    }

    private void initDate() {
        AssetManager assetManager = this.getAssets();
        String json = getFromAssets("data1.json");
        nodes = gson.fromJson(json, new TypeToken<ArrayList<QANode>>() {
        }.getType());
        int totalNumber = getTotalNumber(nodes);
        setQuestionNumber(totalNumber, nodes);
    }

    /**
     * 设置问题题号
     *
     * @param totalNumber
     * @param nodes
     */
    private void setQuestionNumber(int totalNumber, ArrayList<QANode> nodes) {
        int preIndex = 1;
        for (int i = 0; i < nodes.size(); i++) {
            if (!nodes.get(i).mTemplate.equals(EXELContaint.MULTI_QUESTION)) {
                nodes.get(i).setPostfixNumber(totalNumber);
                nodes.get(i).setPrefixNumber(preIndex++);
            } else {
                if (isChildrenCountOne(nodes.get(i))) {
                    preIndex++;
                    for (int j = 0; j < nodes.get(i).mChildren.size(); j++) {
                        nodes.get(i).mChildren.get(j).setPostfixNumber(nodes.get(i).mChildren.size());
                        nodes.get(i).mChildren.get(j).setPrefixNumber(j + 1);
                    }
                } else {
                    for (int k = 0; k < nodes.get(i).mChildren.size(); k++) {
                        nodes.get(i).mChildren.get(k).setPostfixNumber(totalNumber);
                        nodes.get(i).mChildren.get(k).setPrefixNumber(i + k + 1);

                    }
                    preIndex = preIndex + nodes.get(i).mChildren.size();
                }
            }
        }
    }

    /**
     * 获取总题数
     *
     * @param nodes
     * @return
     */
    private int getTotalNumber(ArrayList<QANode> nodes) {
        int total = 0;
        for (QANode node : nodes) {
            if (!node.mTemplate.equals(EXELContaint.MULTI_QUESTION)) {
                total++;
            } else {
                if (isChildrenCountOne(node)) {
                    total++;
                } else {
                    total = total + node.mChildren.size();
                }
            }
        }
        return total;
    }

    /**
     * 子题是否算一题
     *
     * @param node
     * @return
     */
    private boolean isChildrenCountOne(QANode node) {
        //TODO 判断依据按真实需求定
        if (node.mType.equals(EXELContaint.CLOZE_QUESTION)) {
            return true;
        } else {
            return false;
        }
    }

    public String getFromAssets(String fileName) {
        try {
            InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String Result = "";
            while ((line = bufReader.readLine()) != null)
                Result += line;
            return Result;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }
}

